<!--
 * 参数设置
 * @author: hexiaozhen
 * @since: 2023-09-27
 * index.vue
-->
<template>
  <div class="main-content">
    <!-- 搜索 -->
    <TableForm
      ref="formAndTableRef"
      :formItems="formItems"
      :formData="formData"
      :girdOption="girdOption"
      class="tableForm"
      @getTableList="getTableList"
      @selectionChange="selectionChange"
      @handleBtnClick="handleBtnClick"
    >
      <!-- 自定义操作按钮 -->
      <template #tableOptate="{ record }">
        <a-button
          v-for="clickItem in tableOperateList"
          :key="clickItem.text"
          type="text"
          class="operate-item"
          :status="clickItem.eventName == 'tableDel' ? 'danger' : ''"
          @click="handleBtnClick(clickItem.eventName, record)"
        >
          {{ clickItem.text }}
        </a-button>
      </template>
      <!-- 列插槽 -->
      <template #createTime="{ record }">
        {{ record.createTime.slice(0, 10) }}
      </template>
      <template #configType="{ record }">
        {{ record.configType === 'Y' ? '是' : '否' }}
      </template>
    </TableForm>
    <AddEditConfig v-if="visible" v-model:visible="visible" :rowData="rowData" @submit="getTableList()"></AddEditConfig>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import TableForm from '@/components/tableForm/index.vue'
import AddEditConfig from './components/addEditConfig.vue' // 新增编辑
import system from '@/api/modules/system'
import { Modal, Message } from '@arco-design/web-vue'

const formData = reactive({ configName: '', configKey: '', configType: '', dateRange: ['', ''] })
const formAndTableRef = ref()
// 获取字典数据
const configTypeOptions = ref([])
// 表单配置
const formItems = reactive([
  { name: 'configName', label: '参数名称', type: 'a-input' },
  { name: 'configKey', label: '参数键名', type: 'a-input' },
  { name: 'configType', label: '系统内置', type: 'a-select', options: configTypeOptions },
  { name: 'dateRange', label: '创建时间', type: 'a-range-picker' },
])

const girdOption = reactive({
  btnList: [
    { name: '新增', eventName: 'add', type: 'icon-xinzeng' },
    { name: '导出', eventName: 'export', type: 'icon-daochu' },
    { name: '删除', status: 'danger', eventName: 'delMultiple', type: 'icon-shanchu' },
    { name: '刷新缓存', eventName: 'refreshCache', type: 'icon-shuaxin' },
  ],
  columns: [
    { title: '参数主键', dataIndex: 'configId', width: 100 },
    { title: '参数名称', dataIndex: 'configName', width: 100 },
    { title: '参数键名', dataIndex: 'configKey', width: 190 },
    { title: '参数键值', dataIndex: 'configValue', width: 200 },
    { title: '系统内置', slotName: 'configType', width: 98 },
    { title: '备注', dataIndex: 'remark', width: 180 },
    { title: '创建时间', slotName: 'createTime', width: 124 },
    { title: '操作', slotName: 'optional', width: 110, fixed: 'right' },
  ],
  loading: false,
  tableSlotList: ['createTime', 'configType'], //表格插槽
  tableId: 'configId', //表格唯一标识符类似于id
  rowSelection: true, // 多选
})

// 表格操作插槽
const tableOperateList = reactive([
  { text: '修改', eventName: 'tableEdit' },
  { text: '删除', eventName: 'tableDel' },
])
let visible = ref(false) // 新增编辑弹窗
let rowData = reactive({ type: 'add' }) // 弹窗数据

let selectionList = [] // 多选数据
const handleBtnClick = (type, row) => {
  switch (type) {
    case 'add': {
      // 新增
      rowData = { type: 'add' }
      visible.value = true
      break
    }
    case 'delMultiple': {
      //批量删除
      if (!selectionList.length) {
        return Message.warning('请先选择表格数据！')
      }
      Modal.open({
        title: '警告',
        content: `是否确认批量删除已选数据项？`,
        draggable: true,
        onOk: () => {
          delConfig(selectionList.join(','))
        },
      })
      break
    }
    case 'tableEdit': {
      //编辑
      visible.value = true
      rowData = { ...row, type: 'edit' }
      break
    }
    case 'export': {
      // 导出
      break
    }
    case 'refreshCache': {
      // 刷新缓存
      break
    }
    case 'tableDel': {
      //删除
      Modal.open({
        title: '警告',
        content: `是否确认删除参数名称为${row.configName}的数据项？`,
        draggable: true,
        onOk: () => {
          delConfig(row.configId)
        },
      })
      break
    }
  }
}
const delConfig = (id) => {
  system.delConfig(id).then((res) => {
    getTableList()
    Message.success(res.msg)
  })
}
// 获取表格数据
let requestParam = {}
const getTableList = (pageConfig) => {
  if (pageConfig) {
    requestParam.size = pageConfig.pageSize
    requestParam.current = pageConfig.current
  }
  let param = {
    ...formData,
  }
  param.createTimeStart = param.dateRange[0].length ? param.dateRange[0] + ' 00:00:00' : ''
  param.createTimeStartEnd = param.dateRange[1].length ? param.dateRange[1] + ' 23:59:59' : ''
  delete param.dateRange
  girdOption.loading = true
  system
    .getConfig(param, requestParam)
    .then((res) => {
      formAndTableRef.value.setTableData(res.data.records, Number(res.data.total))
      girdOption.loading = false
    })
    .catch(() => {
      girdOption.loading = false
    })
}
// 多选
const selectionChange = (list) => {
  selectionList = list
}
// 数据字典
const getOption = () => {
  const param = { dictTypes: 'sys_yes_no' }
  system.getDicts(param).then((res) => {
    configTypeOptions.value = res.data.sys_yes_no.map((it) => ({
      label: it.dictLabel,
      value: it.dictValue,
    }))
  })
}
onMounted(() => {
  getOption()
  getTableList({ pageSize: 10, current: 1 })
})
</script>
